package day8;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		ArrayList<Student> list= new ArrayList<Student>();
		while(true) {
			System.out.println("是否输入学生信息：");
			System.out.println("输入：1");
			System.out.println("退出：0");
			int judge =scanner.nextInt();
			if (judge == 0) {
				break;
			}
			if (judge == 1) {
				System.out.println("请输入学生的id：");
				int id = scanner.nextInt();
				System.out.println("请输入学生的名字：");
				scanner.nextLine();// 先处理缓冲区里的内容
				String name = scanner.nextLine();
				System.out.println("请输入学生的年龄：");
				int age = scanner.nextInt();
				System.out.println("请输入学生的性别：");
				String gender = scanner.next();
					
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
		}
		for (Student student : list) {
			System.out.println(student);
		}
		System.out.println("----------------------------------------");
		while(true) {
			System.out.println("根据id来搜索：1");
			System.out.println("根据name来搜索：2");
			System.out.println("根据age来搜索：3");
			System.out.println("根据gender来搜索：4");
			System.out.println("退出：0");
			int type = scanner.nextInt();
			if (type == 0) {
				break;
			}
			switch (type){
			case 1:
				System.out.println("请输入查询学生的id：");
				int id = scanner.nextInt();
				searchById(id, list);
				break;
			case 2:
				System.out.println("请输入查询学生的名字：");
				String name = scanner.next();
				searchByName(name, list);
				break;
			case 3:
				System.out.println("请输入查询学生的年龄：");
				int age = scanner.nextInt();
				searchByAge(age, list);
				break;
			case 4:
				System.out.println("请输入查询学生的性别：");
				String gender = scanner.next();
				searchByGender(gender, list);
				break;
			}
		}
	}

	private static void searchByAge(int age, ArrayList<Student> list) {
		boolean isfoundage = false;
		for (Student student : list) {
			if (age == student.getAge()) {
				System.out.println(student);
				isfoundage = true;
			}
		}
		if (isfoundage == false) {
			System.out.println("未找到该学生");
		}
	}

	private static void searchById(int id, ArrayList<Student> list) {
		boolean isfoundid = false;
		for (Student student : list) {
			if (id == student.getId()) {
				System.out.println(student);
				isfoundid = true;
			}
		}
		if (isfoundid == false) {
			System.out.println("未找到该学生");
		}
	}

	private static void searchByGender(String gender, ArrayList<Student> list) {
		boolean isfoundgender = false;
		for (Student student : list) {
			if (gender.equals(student.getGender())) {
				System.out.println(student);
				isfoundgender = true;
			}
		}
		if (isfoundgender == false) {
			System.out.println("未找到该学生");
		}
	}
	
	public static void searchByName(String name, ArrayList<Student> list) {
		boolean isfoundname = false;
		for (Student student : list) {
			if (name.equals(student.getName())) {
				System.out.println(student);
				isfoundname = true;
			}
		}
		if (isfoundname == false) {
			System.out.println("未找到该学生");
		}
	
	}
}
